
import localforage from "localforage"
localforage.config({
  // driver: [localforage.INDEXEDDB],
  name: 'INDEXDB',
  version: "1.0"
})

export default {
  async create(data) {
    data = JSON.parse(JSON.stringify(data))
    let workers = await localforage.getItem("worker") || new Map()
    // 生成id
    let keys = [...workers.keys()].sort((a, b) => a - b)
    let leng = keys.length ? keys[keys.length - 1] : 0
    let uid = data.uid ? data.uid : leng + 1
    data.uid = uid
    workers.set(uid, data)
    return localforage.setItem("worker", workers)
  },
  /**
   * type 
   * remarks备注
   * workerName 员工姓名
   * idCardNumber 身份证号
   */
  // 类别字段线上没有
  async query({type, content,}) {
    let workers = await localforage.getItem("worker") || new Map()
     let data = type ? [...workers.values()].filter(item => item[type].includes(content)) : [...workers.values()]
     return data
  },

  async delete(keys) {
    let workers = await localforage.getItem("worker") || new Map()
    let a = keys.forEach(item => workers.delete(item))
    return localforage.setItem("worker", workers)
  },

  // 卡片变更
  async updateCard ({id, cardid}) {
    let workers = await localforage.getItem("worker") || new Map()
    workers.forEach((item, key) => {
      console.log(item, key)
      if (id == key) {
        let obj = workers.get(key)
        obj.cardid = cardid
        workers.set(key, obj)
      }
    })
    return localforage.setItem("worker", workers)
  }
}